//+------------------------------------------------------------------+
//|                                                  TimedClose.mq4 |
//|                        Copyright 2023, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023"
#property version   "1.00"
#property strict

// 输入参数
extern int    CloseHour    = 14;     // 平仓小时（服务器时间）
extern int    CloseMinute  = 0;      // 平仓分钟（服务器时间）
extern bool   ClosePending = false;  // 是否删除挂单

// 全局变量
bool isClosed = false;               // 当日平仓标志

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    EventSetTimer(60);  // 设置每分钟检查一次时间
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    EventKillTimer();  // 移除定时器
}

//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
{
    datetime serverTime = TimeCurrent();
    int currentHour = TimeHour(serverTime);
    int currentMin = TimeMinute(serverTime);
    
    // 检查是否到达指定时间
    if(currentHour == CloseHour && currentMin == CloseMinute)
    {
        if(!isClosed)
        {
            CloseAllMarketOrders();  // 平仓所有市价单
            if(ClosePending) DeletePendingOrders();  // 删除挂单
            isClosed = true;
        }
    }
    else
    {
        // 重置平仓标志
        isClosed = false;
    }
}

//+------------------------------------------------------------------+
//| 平仓所有市价单函数                                               |
//+------------------------------------------------------------------+
void CloseAllMarketOrders()
{
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if(OrderType() == OP_BUY)
            {
                OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrNONE);
            }
            else if(OrderType() == OP_SELL)
            {
                OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrNONE);
            }
        }
    }
}

//+------------------------------------------------------------------+
//| 删除所有挂单函数                                                 |
//+------------------------------------------------------------------+
void DeletePendingOrders()
{
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if(OrderType() > OP_SELL)  // 挂单类型
            {
                OrderDelete(OrderTicket());
            }
        }
    }
}
//+------------------------------------------------------------------+